app.controller("z-employeeCorporation-c", [ '$scope','$rootScope','$z-http','$sce','z-employeeCorporation-s','z-dictionary-s','z-corporation-s','z-edit-menu-s','z-employee-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,DictionaryService,CorporationService,MenuService,EmployeeService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";
		$scope.viewModel.type=1;
		
		$scope.editModel = {};
		// 保存所选公司
		$scope.editModel.choosedCorporation = [];
		$scope.editModel.employeeCorporation= {};
		$scope.editModel.employeeCorporation.corporationIdList=[];
		$scope.editModel.employeeCorporation.employeeId = null;
		//查询页面名称
		MenuService.getMenuByUrl({url:'employeeCorporation'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载
		$scope.viewModel.bindData = function(){
			$scope.viewModel.employeeList=[];
			if($scope.viewModel.employeeNo!=null){
				var employeeNoData = $scope.viewModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getEmployeeCorporationList({
				employeeList:$scope.viewModel.employeeList,
				perPage:$scope.viewModel.rowCount,
				rowCount:$scope.viewModel.perPage
			},
			function(data){
				$scope.viewModel.dataList = data.employeeBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		$scope.viewModel.bindData();
				
		//新增
		$scope.viewModel.add = function(){
			$scope.viewModel.type=1;
			$scope.viewModel.load();
			$scope.editModel.title="新增分配";
			$scope.editModel.type = true;
			$scope.editModel.choosedCorporation=[];
			$scope.editModel.employeeCorporation={};
			$scope.editModel.employeeCorporation.corporationBeanList=[];
		};
		//修正
		$scope.viewModel.update=function(employee){
			$scope.viewModel.type=2;
			$scope.viewModel.load();
			$scope.editModel.title="分配公司";
			$scope.editModel.type = false;
			$scope.editModel.choosedCorporation=[];
			$scope.editModel.employeeCorporation=angular.copy(employee);
			for(var i=0;i<$scope.editModel.employeeCorporation.corporationBeanList.length;i++){
				$scope.editModel.choosedCorporation.push($scope.editModel.employeeCorporation.corporationBeanList[i]);
			}
		};
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};
		
		//验证
		$scope.viewModel.requiredForm= function() {
		    //验证员工是否选择
		    if($scope.editModel.employeeCorporation.employeeId==null){
			    $rootScope.alertWarning("未选择员工，请选择！");
			    return true;
		    }else{
		    	return false;
		    }
	    };
		
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			var employeeCorporation={};
			employeeCorporation.corporationBeanList=angular.copy($scope.editModel.choosedCorporation);
			employeeCorporation.employeeId=$scope.editModel.employeeCorporation.employeeId;
			$scope.viewModel.isLoading = true;
			Service.updateEmployeeCorporation({
				employeeId:employeeCorporation.employeeId,
				corporationBeanList:employeeCorporation.corporationBeanList,
				type:$scope.viewModel.type,
			},
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("分配公司成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
		};
		
		//删除选中公司
		$scope.editModel.delCorporation=function(corporation){
			$rootScope.alertConfirm("确定删除吗？",function() {
				for(var i = 0; i < $scope.editModel.choosedCorporation.length; i++){
					if($scope.editModel.choosedCorporation[i].corporationId == corporation.corporationId){
						$scope.editModel.choosedCorporation.splice(i,1);
					}
				}
			});
		};
	
		//查询权限是否选中
		$scope.editModel.checkbox=function(corporation,value){
			if(value==0){
				if(corporation.isApproval==0 || corporation.isApproval==1){
					return true;
				}
			}else{
				if(corporation.isApproval==1){
					return true;
				}
			}
			return false;
		};
		
		//是否选中
		$scope.editModel.checkboxClick=function(corporation){
			if(corporation.isApproval==0){
				corporation.isApproval=1;
			}else{
				corporation.isApproval=0;
			}
		};
		
		/*****公司选择*****/
		$scope.viewCorporationModel={};
		$scope.viewCorporationModel.dataList={};
		$scope.viewCorporationModel.corporation={};
		$scope.viewCorporationModel.checkboxDataList=[];
		$scope.viewSelectCorporationModel={};
		$scope.viewCorporationModel.effectiveDate = new Date();
		$scope.viewSelectCorporationModel.show=false;
		
		$scope.viewCorporationModel.bindData = function(){
			$scope.viewCorporationModel.isLoading=true;
			//获取基本信息
			CorporationService.getCorporationList({
				fromDate:$scope.viewCorporationModel.effectiveDate,
				employeeId:$scope.editModel.employeeCorporation.employeeId,
			},
			function(data){
				$scope.viewCorporationModel.dataList = data;
				$scope.viewCorporationModel.addCheckbox();
				$scope.viewCorporationModel.loadDefaultcheckbox();
			    $scope.viewCorporationModel.JudgeCheckboxAll();
				$scope.viewCorporationModel.isLoading=false;
			},function(error) {
				$scope.viewCorporationModel.isLoading=false;
			});
		};
		
		$scope.viewSelectCorporationModel.load=function(){
			$scope.viewSelectCorporationModel.show=true;
		};
		
		//关闭加载
		$scope.viewSelectCorporationModel.unLoad=function(){
			$scope.viewSelectCorporationModel.show=false;
		};
		
		// 公司选择按钮
		$scope.viewCorporationModel.chooseUpde = function(){
			$scope.viewSelectCorporationModel.show=true;
			$scope.viewSelectCorporationModel.title="公司选择";
			$scope.viewCorporationModel.checkAll = false;
			$scope.viewCorporationModel.checkboxDataList = [];
			$scope.viewCorporationModel.bindData();
		};
		
		// 点击保存选择公司按钮
		$scope.viewSelectCorporationModel.save = function(){
			for(var i=0;i<$scope.viewCorporationModel.checkboxDataList.length;i++){
				var type=true;
				for(var j=0;j<$scope.editModel.choosedCorporation.length;j++){
					if($scope.viewCorporationModel.checkboxDataList[i].corporationId==$scope.editModel.choosedCorporation[j].corporationId){
						type=false;
						break;
					}else{
						type=true;
					}
				}
				if(type){
					var data={};
					data.corporationId=$scope.viewCorporationModel.checkboxDataList[i].corporationId;
					data.corporationCode=$scope.viewCorporationModel.checkboxDataList[i].corporationCode;
					data.corporationName=$scope.viewCorporationModel.checkboxDataList[i].corporationName;
					data.isApproval=0;
					$scope.editModel.choosedCorporation.push(data);
				}
			}
			$scope.viewSelectCorporationModel.show=false;
		};
		
		$scope.viewCorporationModel.isInAssignmentArr=function(corporation){
			if($scope.editModel.employeeCorporation.corporationId == corporation.corporationId){
				return true;
			}
			return false;
		};
		
		//处理加载复选框选择状态
		$scope.viewCorporationModel.loadDefaultcheckbox=function(){
		    //循环判断是否为默认公司
		   for(var i=0;i<$scope.editModel.choosedCorporation.length;i++){
		    	for(var j=0;j<$scope.viewCorporationModel.dataList.length;j++){
		    		if($scope.editModel.choosedCorporation[i].corporationId==$scope.viewCorporationModel.dataList[j].corporationId){
		    			$scope.viewCorporationModel.dataList[j].checkbox=true;
		    			break;
		    		}
		    	}
		    }
		};
		
		//设置默认复选框值
		$scope.viewCorporationModel.addCheckbox=function(){
			 for(var i=0;i<$scope.viewCorporationModel.dataList.length;i++){
				 $scope.viewCorporationModel.dataList[i].checkbox=false;
			 }
		};
		
		//判断是否为全选
		$scope.viewCorporationModel.JudgeCheckboxAll=function(){
			 var type=false;
			 for(var i=0;i<$scope.viewCorporationModel.dataList.length;i++){
				 if($scope.viewCorporationModel.dataList[i].checkbox==false){
					 type=false;
					 break;
				 }else{
					 type=true;
				 }
			 }
			 if(type){
				 $scope.viewCorporationModel.checkAll=true;
			 }else{
				 $scope.viewCorporationModel.checkAll=false;
			 }
		};

		//全选事件
		$scope.viewCorporationModel.checkboxAll=function(){
			//判断是否为全选
			 if($scope.viewCorporationModel.checkAll==false){
				 angular.forEach($scope.viewCorporationModel.dataList, function(value, key) {
					 value.checkbox=true;
					 var type=true;
					 for(var i=0;i<$scope.viewCorporationModel.checkboxDataList.length;i++){
						 if($scope.viewCorporationModel.checkboxDataList[i].corporationId==value.corporationId){
							 type=false;
							 break;
						 }else{
							 type=true;
						 }
					 }
					 if(type){
						 $scope.viewCorporationModel.checkboxDataList.push(value);
					 }
				 });
			 }else{
				 angular.forEach($scope.viewCorporationModel.dataList, function(value, key) {
					 var type=true;
					 for(var i=0;i<$scope.editModel.employeeCorporation.corporationBeanList.length;i++){
						if(value.corporationId==$scope.editModel.employeeCorporation.corporationBeanList[i].corporationId){
							if($scope.editModel.employeeCorporation.corporationBeanList[i].fromDate!=null){
								type=false;
								break;
							}
						}
					 }
					 if(type){
						 value.checkbox=false;
						 for(var i=0;i<$scope.viewCorporationModel.checkboxDataList.length;i++){
							 if($scope.viewCorporationModel.checkboxDataList[i].corporationId==value.corporationId){
								 $scope.viewCorporationModel.checkboxDataList.splice(i,1);
							 }
						 }
					 }
				 });
			 }
		};
		
		//单选事件
		$scope.viewCorporationModel.checkbox=function(corporation){
			 if(corporation.checkbox){
				 corporation.checkbox=false;
				 for(var i=0;i<$scope.viewCorporationModel.checkboxDataList.length;i++){
					 if($scope.viewCorporationModel.checkboxDataList[i].employeeId==corporation.employeeId){
						 $scope.viewCorporationModel.checkboxDataList.splice(i,1);
					 }
				 }
			 }else{
				 corporation.checkbox=true;
				 $scope.viewCorporationModel.checkboxDataList.push(corporation);
			 }
			 $scope.viewCorporationModel.JudgeCheckboxAll();
		};
		
		//是否可用
		$scope.viewCorporationModel.disabled=function(corporation){
			if($scope.editModel.choosedCorporation!=null){
				for(var i=0;i<$scope.editModel.choosedCorporation.length;i++){
					if(corporation.corporationId==$scope.editModel.choosedCorporation[i].corporationId){
						return true;
					}
				}
			}
			return false;
		};
		
		/*   人员选择   */
		$scope.viewEmployeeModel={};
		$scope.viewEmployeeModel.perPage = "10";
		$scope.viewEmployeeModel.rowCount = "1";
		// 人员选择按钮
		$scope.editModel.chooseUpde = function(){
			$scope.viewEmployeeModel.show=true;
			$scope.viewEmployeeModel.title="员工选择";
			$scope.viewEmployeeModel.employeeIsSeparated=null;
			$scope.viewEmployeeModel.regionType=null;
			$scope.viewEmployeeModel.employeeNo=null;
			$scope.viewEmployeeModel.roleName=null;
			$scope.viewEmployeeModel.departmentList=[];
			$scope.viewEmployeeModel.corporationList=[];
			$scope.viewEmployeeModel.employeeList=[];
			$scope.viewEmployeeModel.level=null;
			$(".multiselect-selected-text").text("无");
			//下拉多选框长度
			$scope.multiSelectMaxHeight=300;
			//加载信息
			$scope.viewEmployeeModel.bindSelectRegionTypeData = function(){
				//查询公司
				CorporationService.getCorporationList({
					fromDate:$scope.viewModel.effectiveDate,
				},
				function(data){
					var site = [];  
			        $.each(data, function(index, values) {  
			            site.push({  
			                label : "("+values.corporationCode+")"+values.corporationName,  
			                value : values.corporationCode  
			            });  
			        });  
			        $('#ectCorporation').multiselect('dataprovider',site);
				},function(error) {
					
				});
			};
			$scope.viewEmployeeModel.bindSelectRegionTypeData();
			
		
			/*******使用为下拉带勾选控件*********/
			//员工级别
			$('#ectlevel').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司
			$('#ectCorporation').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司选中事件
			$("#ectCorporation").change(function(){  
				var value=$(this).val();
			    var data={};
			    data.corporationCodeList=value;
			    data.fromDate = $scope.viewModel.effectiveDate;
			    if(value==null){
			    	var site = [];  
			    	$('#ectDepartmentName').multiselect('dataprovider',site);
			    	$scope.viewEmployeeModel.departmentList=[];
			    }else{
			    	//查询部门
				    DepartmentService.getDepartmentListByCorporationCodeList(data,
					function(data){
				    	var site = [];  
				        $.each(data, function(index, values) {  
				            site.push({  
				                label : "("+values.departmentCode+")"+values.departmentName,  
				                value : values.departmentCode  
				            });  
				        });  
				        $('#ectDepartmentName').multiselect('dataprovider',site);
					},function(error) {
						
					});
			    }
	        });  
			
			//部门
			$('#ectDepartmentName').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			EmployeeService.getEmployeeList({
				rowCount : $scope.viewEmployeeModel.perPage,
				perPage : $scope.viewEmployeeModel.rowCount,
				fromDate:$scope.viewModel.effectiveDate,
			},
			function(data){
				$scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewEmployeeModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewEmployeeModel.isLoading=false;
			});
			
		}
		//关闭加载
		$scope.viewEmployeeModel.unLoad=function(){
			$scope.viewEmployeeModel.show=false;
			$scope.editModel.title="新增分配";
		};
		//是否选中
		$scope.viewEmployeeModel.isInAssignmentArr=function(employee){
			if($scope.editModel.employeeCorporation.employeeId == employee.employeeId){
				return true;
			}
			return false;
		};
		//加载数据
		$scope.viewEmployeeModel.bindData = function(){
			$scope.viewEmployeeModel.employeeList=[];
			if($scope.viewEmployeeModel.employeeNo!=null){
				var employeeNoData = $scope.viewEmployeeModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewEmployeeModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			EmployeeService.getEmployeeList({
				employeeList:$scope.viewEmployeeModel.employeeList,
				departmentList:$scope.viewEmployeeModel.departmentList,
				corporationList:$scope.viewEmployeeModel.corporationList,
				employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,
				employeeIsSeparated:"0",
				perPage:$scope.viewEmployeeModel.rowCount,
				rowCount:$scope.viewEmployeeModel.perPage,
				fromDate:$scope.viewModel.effectiveDate,
			},
			function(data){
				$scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		// 点击选择指定人员按钮
		$scope.viewEmployeeModel.employee = function(employee){
			$scope.viewEmployeeModel.show=false;
			$scope.viewModel.show=true;
			$scope.editModel.employeeCorporation.employeeId = employee.employeeId;
			$scope.editModel.employeeCorporation.employeeNo = employee.employeeNo;
			$scope.editModel.employeeCorporation.chineseName = employee.chineseName;
		}
		// 员工清空
		$scope.editModel.chooseDelete = function(){
			$scope.editModel.employeeCorporation.employeeId = null;
			$scope.editModel.employeeCorporation.employeeNo = null;
			$scope.editModel.employeeCorporation.chineseName = null;
		}
} ]);